<%@ WebHandler Language="C#" Class="SouthernLand.RaoVat.Img" %>

// Copyright (C) 2005 by Greg Ennis
// (mailto:southernland@yahoo.com)
// The contents of this file are subject to the Artistic License (the "License").
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at:


using System.Web;
using System.IO;
using System.Drawing;
using SouthernLand.Common;

namespace SouthernLand.RaoVat
{
	public class Img : IHttpHandler 
	{ 
		public bool IsReusable 
		{ get { return true; } } 

		public void ProcessRequest(HttpContext Context) 
		{
            // Writing an image to output stream
            Context.Response.ContentType = "image/jpg";

            string ImageUrl = "";
            string FilePath = "";
            int LoaitinID = -1;
            
            ImageUrl = Globals.GetStringFromQueryString("ImageUrl");

            if (ImageUrl.Length > 3)
                FilePath = Globals.PhysicalApplicationPath() + "UserUpload/NhaDat/"+ ImageUrl.ToString();
            else
            {
                LoaitinID = Globals.GetIntFromQueryString("LoaitinID");
                if (LoaitinID == (int)eLoaitin.CanBan)
                    FilePath = Globals.PhysicalApplicationPath() + "Images/HinhMinhHoa/CanBan.bmp";
                else if (LoaitinID == (int)eLoaitin.CanMua)
                    FilePath = Globals.PhysicalApplicationPath() + "Images/HinhMinhHoa/CanMua.bmp";
                else if (LoaitinID == (int)eLoaitin.CanThue)
                    FilePath = Globals.PhysicalApplicationPath() + "Images/HinhMinhHoa/CanThue.bmp";
                else if (LoaitinID == (int)eLoaitin.ChoThue)
                    FilePath = Globals.PhysicalApplicationPath() + "Images/HinhMinhHoa/ChoThue.bmp";
            }
            
			// 'thumbnail' means we are requesting a thumbnail
            if (Context.Request["Size"] != null)
			{
                float size = 100.0f ;
                switch (Context.Request.QueryString["Size"])
                {
                    case "S":
                        size = 70.0f;
                        break;
                    case "M":
                        size = 260.0f;
                        break;
                    case "L":
                        size = 400.0f;
                        break;
                }
                
                //float thumbnail = float.Parse(Context.Request["thumbnail"]);
				// Need to load the image, resize it, and stream to the client.
				// Calculate the scale so as not to stretch or distort the image.
			    Bitmap bmp = !string.IsNullOrEmpty(FilePath) ? new Bitmap(FilePath) : new Bitmap(string.Empty);
                if (bmp.Width > size || bmp.Height > size)
                {
                    float scale = size / System.Math.Max(bmp.Height, bmp.Width);
                    System.Drawing.Image thumb = bmp.GetThumbnailImage((int)(bmp.Width * scale), (int)(bmp.Height * scale), null, System.IntPtr.Zero);
                    thumb.Save(Context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    bmp.Dispose();
                    thumb.Dispose();
                }
                else
                    ShowImage(FilePath);
			}
			else
			{
                ShowImage(FilePath);
			}
		}
        
        public void ShowImage(string FilePath)
        {
            // Stream directly from the file
            // Get the stream and send it out the response
            System.IO.FileStream fs = System.IO.File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);

            const int byteLength = 1024 * 16;
            byte[] bytes = new byte[byteLength];
            while (fs.Read(bytes, 0, byteLength) != 0)
            {
                HttpContext.Current.Response.BinaryWrite(bytes);
            }
            fs.Close();
        }
	}
}


